#include "regx51.h"
#include "math.h"



//#define Common_anode    //共阳极
#define Common_cathode  //共阴极



void Delay_ms(unsigned int t);

#ifdef Common_cathode//共阴极段码

unsigned char code LED_Table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
                                //0  1  2  3  4  5  6  7  8  9
                                ,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40,0x00};
                                //A  B  C  D  E  F  -  Down
unsigned char code LED_F_Table[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef
                                //0  1  2  3  4  5  6  7  8  9
                                ,0xf7,0xfc,0xb9,0xde,0xf9,0xf1,0x40,0x00};
                                //A  B  C  D  E  F  -  Down
#endif

#ifdef Common_anode//共阳极段码

unsigned char code LED_Table[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90,
                                  //0  1  2  3  4  5  6  7  8  9
                                  0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e, 0xbf, 0xff};
                                  //A  B  C  D  E  F  -  Down
unsigned char code LED_F_Table[] = {0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x79, 0x00, 0x10,
                                  //0  1  2  3  4  5  6  7  8  9
                                  0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e, 0xbf, 0xff};
                                  //A  B  C  D  E  F  -  Down
#endif

unsigned char data LED_Seg_DisNum[8]={0};

#ifdef Common_cathode//共阴极段码
#define SCAN_START 0xfe
#define SCAN_ADD 1
#endif

#ifdef Common_anode//共阳极段码
#define SCAN_END 0x01
#define SCAN_ADD 0
#endif

void Show_Seg8_Unsigend_Num(unsigned long int num)
{
    unsigned char scan=SCAN_START;
    unsigned char i=0;
    LED_Seg_DisNum[0]=num/10000000;
    num%=10000000;
    LED_Seg_DisNum[1]=num/1000000;
    num%=1000000;
    LED_Seg_DisNum[2]=num/100000;
    num%=100000;
    LED_Seg_DisNum[3]=num/10000;
    num%=10000;
    LED_Seg_DisNum[4]=num/1000;
    num%=1000;
    LED_Seg_DisNum[5]=num/100;
    num%=100;
    LED_Seg_DisNum[6]=num/10;
    num%=10;
    LED_Seg_DisNum[7]=num/1;

    for(i=0;i<8;i++)
    {
        P2=0xff;
        P0=LED_Table[LED_Seg_DisNum[i]];
        P2=scan;
        scan=(scan<<1)+SCAN_ADD;
        Delay_ms(5);
    }

}

void Show_Seg8_Float(float float_num)
{
    unsigned char float_flag=0;
    unsigned char j=0;
    unsigned long int num=0;
    unsigned char scan=SCAN_START;
    unsigned char i=0;
    if(float_num<10)
    {
        while(j<7)
        {
            float_num*=10;
            if(float_num<0)
            float_flag+=1;
            j++;
        }
    }
    else
    {
        if(float_num>=10&&float_num<100)
        {
            float_num*=1000000;
            float_flag=1;
        }
        else if (float_num>=100&&float_num<1000)
        {
            float_num*=100000;
            float_flag=2;
        }
        else if (float_num>=1000&&float_num<10000)
        {
            float_num*=10000;
            float_flag=3;
        }
        else if (float_num>=10000&&float_num<100000)
        {
            float_num*=1000;
            float_flag=4;
        }
        else if (float_num>=100000&&float_num<1000000)
        {
            float_num*=100;
            float_flag=5;
        }
        else if (float_num>=1000000&&float_num<10000000)
        {
            float_num*=10;
            float_flag=6;
        }
        else
        {
            float_num*=1;
            float_flag=8;
        }
    }
    num=(unsigned long int)float_num;
    LED_Seg_DisNum[0]=num/10000000;
    num%=10000000;
    LED_Seg_DisNum[1]=num/1000000;
    num%=1000000;
    LED_Seg_DisNum[2]=num/100000;
    num%=100000;
    LED_Seg_DisNum[3]=num/10000;
    num%=10000;
    LED_Seg_DisNum[4]=num/1000;
    num%=1000;
    LED_Seg_DisNum[5]=num/100;
    num%=100;
    LED_Seg_DisNum[6]=num/10;
    num%=10;
    LED_Seg_DisNum[7]=num/1;

    for(i=0;i<8;i++)
    {
        P2=0xff;
        if(i==float_flag)
        P0=LED_F_Table[LED_Seg_DisNum[i]];
        else
        P0=LED_Table[LED_Seg_DisNum[i]];
        P2=scan;
        scan=(scan<<1)+SCAN_ADD;
        Delay_ms(5);
    }

}

void Timer_Init(void)
{
	TMOD=0x02;
    TL0=0x9c;
    TH0=0x9c;
    ET0=1;
    EA=1;
    TR0=1;
}


void Delay_ms(unsigned int t)
{
    unsigned int i,j;
    for(i=0;i<t;i++)
    {
        for(j=0;j<100;j++)
        {
            ;
        }
    }
}

unsigned long int Swap_Bits(unsigned long int num) reentrant
{
    if(num/2!=0)
    return Swap_Bits(num/2)*10+num%2;
    else
    return num%2;
}


//
unsigned char a=0;
void main()
{
    Timer_Init();

    while(1)
    {
        Show_Seg8_Unsigend_Num(Swap_Bits(a));
    }
}

void Timer0_ISR(void) interrupt 1
{
    static unsigned int Count=0;
    Count++;
    if(Count==10000)
    {
        Count=0;
        a++;
    }
}
